package com.xywy.wjk.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
/**
 * 血压柱状图
 * @author wangwenlong
 *
 */
public class ChartBarView extends View {
    private int mLeftHeight; //左边的高度
    private int mTotleHeight; //总的高度
    private int mRightHeight ;// 右边的高度
    private int totleWidth;//整体宽度
    private int distance; //距离
    private Context context;
    private int centerX;
    private int centerY;
	public ChartBarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		totleWidth = getWidth();
		mTotleHeight = getHeight();
		distance = totleWidth/7;
		this.context = context;
	}

	public ChartBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		totleWidth = getWidth();
		mTotleHeight = getHeight();
		this.context = context;
	}
	
	public ChartBarView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		totleWidth = getWidth();
		mTotleHeight = getHeight();
		this.context = context;
	}

	public int getmLeftHeight() {
		return mLeftHeight;
	}

	public void setmLeftHeight(int mLeftHeight) {
		this.mLeftHeight = mLeftHeight;
		invalidate();
	}

	public int getmTotleHeight() {
		return mTotleHeight;
	}

	public void setmTotleHeight(int mTotleHeight) {
		this.mTotleHeight = mTotleHeight;
		invalidate();
	}

	public int getmRightHeight() {
		return mRightHeight;
	}

	public void setmRightHeight(int mRightHeight) {
		this.mRightHeight = mRightHeight;
		invalidate();
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Paint p = new Paint();
//		canvas.drawColor(Color.BLUE);
		if(distance == 0)
			distance = getWidth()/7;
		if(mTotleHeight == 0)
		mTotleHeight = getHeight();
		centerY = mTotleHeight/2;
		p.setStyle(Style.FILL);
		p.setColor(Color.YELLOW);
		p.setStrokeWidth(20);
		Rect leftRect = new Rect(); //左边的柱子
		leftRect.left = distance;
		leftRect.right = distance+2*distance;
		leftRect.bottom = centerY;
		leftRect.top = centerY- mLeftHeight;
		canvas.drawRect(leftRect, p);
		Rect rightRect = new Rect(); //右边的柱子
		rightRect.left = 4*distance;
		rightRect.right = 6*distance;
		rightRect.bottom = centerY;
		rightRect.top = centerY-mRightHeight;
		canvas.drawRect(rightRect, p);
		Paint linePaint = new Paint();
		linePaint.setColor(Color.BLACK);
		linePaint.setStrokeWidth(10);
		canvas.drawLine(0, centerY+10, totleWidth, centerY+10, linePaint);
		
		Paint textPain = new Paint();
		textPain.setColor(Color.BLACK);
		textPain.setTextSize(40);
		canvas.drawText("健康", getWidth()/2-40, centerY+40, textPain);
		
		Paint buttonP = new Paint();
		buttonP.setStyle(Style.FILL);
		buttonP.setColor(0xff9bd2f1);
		Rect r = new Rect();
		r.left = 80;
		r.right = getWidth()-80;
		r.top = mTotleHeight-125;
		r.bottom = mTotleHeight-30;
		canvas.drawRect(r, buttonP);
		Paint bttextPain = new Paint();
		bttextPain.setColor(Color.WHITE);
		bttextPain.setTextSize(40);
//		canvas.drawText("立即测量", r.left/2-40*2, mTotleHeight-105, bttextPain);
	}
	
	 //转换dip为px 
	 public static int convertDipOrPx(Context context, int dip) { 
	     float scale = context.getResources().getDisplayMetrics().density; 
	     return (int)(dip*scale + 0.5f*(dip>=0?1:-1)); 
	 } 

}
